package com.xiaoshuidi.cloud.module.guanwang.framework.config;

import cn.hutool.core.util.StrUtil;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.tenant.core.context.TenantContextHolder;
import com.xiaoshuidi.cloud.framework.web.core.util.WebFrameworkUtils;
import com.xiaoshuidi.cloud.module.guanwang.controller.admin.guanwang.configvo.WebsiteLinkVO;
import com.xiaoshuidi.cloud.module.guanwang.controller.admin.guanwang.vo.GuanWangConfigListRespVO;
import com.xiaoshuidi.cloud.module.guanwang.framework.enums.GuanWangConfigEnum;
import com.xiaoshuidi.cloud.module.guanwang.service.guanwang.GuanWangConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xiaoshuidi.cloud.module.guanwang.enums.ErrorCodeConstants.ORIGIN_NOT_EXISTS;

@Slf4j
@Component
public class TenantConfiguration extends OncePerRequestFilter implements InitializingBean {

    public static Map<String, Long> map = new HashMap<>();

    @Resource
    private GuanWangConfigService guanWangConfigService;

    @Override
    public void afterPropertiesSet() {
        List<GuanWangConfigListRespVO> listByIndexId = guanWangConfigService.getListByIndexId(GuanWangConfigEnum.WEBSITE_LINK.getIndex());
        for (GuanWangConfigListRespVO guanWangConfigListRespVO : listByIndexId) {
            WebsiteLinkVO websiteLinkVO = (WebsiteLinkVO) guanWangConfigListRespVO.getConfigJson();
            map.put(websiteLinkVO.getUrl(), guanWangConfigListRespVO.getTenantId());
        }
        log.info("[初始化 域名-tenantId 数据] map:{}", map);
    }


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // 如果是web官网请求, 取官网配置的tenantId
        log.info("[WEB官网前端] 请求路径:{}, 域名:{}", request.getServletPath(), request.getHeader("Content-Host"));
        Long tenantId = null;
        if (StrUtil.startWith(request.getServletPath(), "/app-api")) {
            String origin = request.getHeader("Content-Host");
//            if (map.containsKey(origin)) {
//                tenantId = map.get(origin);
//            } else {
//                throw new ServiceException(ORIGIN_NOT_EXISTS);
//            }
            tenantId = Long.parseLong(origin);
        } else {
            tenantId = WebFrameworkUtils.getTenantId(request);
        }
        if (tenantId != null) {
            TenantContextHolder.setIgnore(false);
            TenantContextHolder.setTenantId(tenantId);
        }

        try {
            filterChain.doFilter(request, response);
        } finally {
            TenantContextHolder.clear();
        }
    }
}
